package com.wjw.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.wjw.utils.MD5Util;
import com.wjw.vo.UsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjw.entity.Users;
import com.wjw.dao.UsersMapper;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;


/**
 * @author WuJianWei
 * @date 2022/2/28 20:00
 */
@Service
public class UsersService extends ServiceImpl<UsersMapper, Users> {


    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @param userName
     * @return
     */
    public int findUserByName(String userName) {
        Users users = this.byName(userName);
        if (users == null) {
            return 0;
        }
        return 1;
    }

    /**
     * @param userName
     * @param formPass
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Users registerUser(String userName, String formPass) {
        String salt = UUID.randomUUID().toString().replace("-", "").substring(0, 15);
        String DBPass = MD5Util.formPassToDBPass(formPass, salt);

        baseMapper.registerUser(userName, DBPass, salt);
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getUsername, userName);
        Users users1 = baseMapper.selectOne(wrapper);
        return users1;
    }

    /**
     *
     * @param userName
     * @param formPass
     * @return
     */
    public Users login(String userName, String formPass) {
        Users users = this.byName(userName);
        String salt = users.getSalt();
        if (StringUtils.isEmpty(salt)) {
            return null;
        }
        String DBPass = MD5Util.formPassToDBPass(formPass, salt);

        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getUsername,userName);
        //wrapper.eq(Users::getPassword,DBPass);
        Users users1 = baseMapper.selectOne(wrapper);
        if (users1 != null) {
            Map<Object, Object> login_count = redisTemplate.opsForHash().entries("login_count");
            if (login_count.isEmpty() || login_count.get(userName) == null) {
                login_count.put(userName, 1);
            } else {
                login_count.put(userName, (int) login_count.get(userName) + 1);
            }
            redisTemplate.opsForHash().putAll("login_count", login_count);
        }
        return users1;
    }

    /**
     *
     * @param user
     */
    public void updateUser(Users user){
        baseMapper.updateById(user);
    }




    /**
     *
     * @param name
     * @return
     */
    public Users byName(String name) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getUsername, name);
        Users users = baseMapper.selectOne(wrapper);
        return users;
    }

    /**
     *
     * @param id
     * @return
     */
    public Users userInfo(Integer id){
        Users users = baseMapper.selectById(id);
        return users;
    }


    public Boolean modify(UsersVo vo){
        String salt = UUID.randomUUID().toString().replace("-", "").substring(0, 15);
        String DBPass = MD5Util.formPassToDBPass(vo.getPassword(), salt);

        Users users = new Users();
        users.setUserId(vo.getUserId());
        users.setUsername(vo.getUsername());
        users.setPassword(DBPass);
        users.setSalt(salt);
        users.setEmail(vo.getEmail());
        users.setGender(vo.getGender());
        users.setUserphonenumber(vo.getUserphonenumber());
        users.setNickName(vo.getNickName());

        int i = baseMapper.updateById(users);
        if (i > 0){
            return true;
        }
        return false;
    }
}
